package shajie.shajun.mmhf.View.Fragment;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import shajie.shajun.mmhf.Adapter.MulRecyclerAdapter;
import shajie.shajun.mmhf.Base.BaseFragment;
import shajie.shajun.mmhf.Bean.Beans;
import shajie.shajun.mmhf.Bean2.Product;
import shajie.shajun.mmhf.Bean2.Result;
import shajie.shajun.mmhf.Interface.RecyclerItemClickListener;
import shajie.shajun.mmhf.Public_Data;
import shajie.shajun.mmhf.R;
import shajie.shajun.mmhf.ViewModel.Bmobdata;
import shajie.shajun.mmhf.databinding.FragmentProductDetailBinding;

/**
 * Created by admin on 2017/11/21.
 */

public class Product_Detail_Fragment extends BaseFragment<FragmentProductDetailBinding> implements View.OnClickListener{
    private FragmentProductDetailBinding binding;
    private  List<Beans<Product>> mBeansList=new ArrayList<>();
    private  MulRecyclerAdapter<Product> mProductMulRecyclerAdapter;
    @Override
    public int setlayoutid() {
        return R.layout.fragment_product_detail;
    }
    @Override
    public void init(final FragmentProductDetailBinding binding) {
        this.binding=binding;
        Public_Data.number++;
        binding.setOnclicklistener(this);
        GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),2);
        binding.recyclerProductDetail.setLayoutManager(layoutManager);

        Bmobdata.getproductdetail(getContext().getApplicationContext(),new Bmobdata.Singer<List<List<Product>>>() {
            @Override
            public void success(List<List<Product>> s) {
                if (getContext()==null||getContext().getApplicationContext()==null)
                    return;
                mBeansList.clear();
                for (int i = 0; i <s.size() ; i++) {
                    mBeansList.add(new Beans<>(R.layout.item_product_detail,s.get(i)));
                }

                mProductMulRecyclerAdapter=new MulRecyclerAdapter<>(getContext(),mBeansList.get(0));
                binding.recyclerProductDetail.addItemDecoration(new DividerItemDecoration(getContext().getApplicationContext(), LinearLayout.VERTICAL));
                binding.recyclerProductDetail.setAdapter(mProductMulRecyclerAdapter);
            }

            @Override
            public void faile(String s) {
                    toast(s);
            }
        });
        binding.recyclerProductDetail.addOnItemTouchListener(new RecyclerItemClickListener(getContext().getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, final int position) {
                final EditText input = new EditText(getContext().getApplicationContext());
                input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                input.setTextColor(Color.BLACK);

                final Product mProduct=((MulRecyclerAdapter)binding.recyclerProductDetail.getAdapter()).getItemProduct(position);
                if (mProduct.getInventory()<0){
                    Toast.makeText(getContext().getApplicationContext(), "库存小于0？有问题联系管理员", Toast.LENGTH_SHORT).show();
                }
                input.setText(String.valueOf(mProduct.getInventory()));
                new AlertDialog.Builder(getContext())
                        .setTitle("库存修改")
                        .setView(input)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        final String value = input.getText().toString();
                                        Bmobdata.modifyDatabase(getContext(), value, mProduct.getProduct(), new Bmobdata.Singer<Result>() {
                                            @Override
                                            public void success(Result s) {
                                                if (!s.getResult().equals("True")){
                                                    Toast.makeText(getContext(), "修改失败!", Toast.LENGTH_SHORT).show();
                                                    return;
                                                }
                                                ((MulRecyclerAdapter)binding.recyclerProductDetail.getAdapter()).setItemProduct(position,Integer.valueOf(value));
                                            }
                                            @Override
                                            public void faile(String s) {

                                            }
                                        });

                                    }
                                })
                        .create()//设置取消按钮并创建
                        .show();//显示
            }

            @Override
            public void onLongClick(View view, int posotion) {

            }
        }));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button0:
                mProductMulRecyclerAdapter.changelist(mBeansList.get(0));
                break;
            case R.id.button1:
                mProductMulRecyclerAdapter.changelist(mBeansList.get(1));
                break;
            case R.id.button2:
                mProductMulRecyclerAdapter.changelist(mBeansList.get(2));
                break;
            case R.id.button3:
                mProductMulRecyclerAdapter.changelist(mBeansList.get(3));
                break;
            case R.id.button4:
                mProductMulRecyclerAdapter.changelist(mBeansList.get(4));
                break;
        }
    }

    @Override
    public void onDestroy() {
        Public_Data.number--;
        mBeansList=null;
        mProductMulRecyclerAdapter=null;
        super.onDestroy();
    }
}
